<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>StudentWorld</title>
    <link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<?php
//naredi include, mora biti {}

require_once 'projekt_st/spletne/controller/OsebaController.php';

$streznik = $_SERVER["HTTP_HOST"];  
			
$pot = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");

include_once("seja.php"); 
{
    
    $controller = new OsebaController();
    
    
    //print_r($vseMape); 
    
    //print_r($_SESSION);
    
    //$vseMape = $controller->getAllMapeControl($_SESSION['uporabnik']['idoseba']);
    
    if(isset($_POST['ustvari'])) {
        //print_r($_SESSION['uporabnik']['idoseba']);
        //print_r($_POST);
        array_pop($_POST);
        mkdir("upload/".$_SESSION['uporabnik']['upIme']."/".$_POST['imeMape'],0777,true);
         
        $data = $_POST;
        $data['oseba_idoseba'] = $_SESSION['uporabnik']['idoseba']; 
        $controller->setMapa($data);            
    }
    
    if(isset($_GET['vseMape'])) {
        
        $mapa = $controller->getMapaControl($_GET['vseMape']);
            
            //print_r($mapa);
            foreach($mapa as $map) {
                $imeMape = $map->getImeMapa();
            }
            
            
        
        $datoteke = $controller->getAllDatotekeControl($_GET['vseMape']);
        //print_r($datoteke);  
    }
    
    if(isset($_POST['nalozi'])) {
        //print_r($_POST);
        //print_r($_FILES);
        if($_FILES['datoteka']['error'] > 0) {
            header("Location: http://".$streznik."".$pot."/datoteka.php?napaka=niDatoteke");
        } else {
            array_pop($_POST);
            $data['mapa_idmapa'] = $_POST['vseMape'];
            $data['imeDatoteke'] = $_FILES['datoteka']['name'];
            
            print_r($data);
            
            $mapa = $controller->getMapaControl($_POST['vseMape']);
            
            //print_r($mapa);
            foreach($mapa as $map) {
                $imeMape = $map->getImeMapa();
            }
            
            if (file_exists("upload/".$_SESSION['uporabnik']['upIme']."/".$imeMape."/".$_FILES["datoteka"]["name"]))
              {
              header("Location: http://".$streznik."".$pot."/datoteka.php?napaka=zeObstaja");
              }
            else
              {
            
            print_r($data);
            
            $controller->setDatoteka($data);
            
            move_uploaded_file($_FILES["datoteka"]["tmp_name"],
            "upload/".$_SESSION['uporabnik']['upIme']."/".$imeMape."/".$_FILES["datoteka"]["name"]);
            
            
            //header("Location: http://".$streznik."".$pot."/datoteka.php?napaka=jeDatoteka");
            }
        }     
    }
    
    $vseMape = $controller->getAllMapeControl($_SESSION['uporabnik']['idoseba']);
    
?>

    <div id="wrapper">
    	<div id="header-wrapper">
        
    		<?php include_once("glava.php"); ?>
            
    	</div>
    	<!-- end #header -->
    	<div id="page">
    		<div id="page-bgtop">
    			<div id="page-bgbtm">
    				<div id="content">
    					<div class="post">    						
    						<div style="clear: both;">&nbsp;</div>
    						<div class="entry">
                                <p><form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
                                        <div>
                                        
                                        <h1>Ustvari mapo</h1>
                                        <table>
                                            <tr>
                                                <td>Ime mape:</td>
                                                <td><input type="text" name="imeMape" id="imeMape" value="" /></td>
                                            </tr>
                                        </table>
                                		<p><input type="submit" id="ustvari" name="ustvari" value="Ustvari" /></p>
                                        </div>
                                	</form></p>
                                    <br><br><br><h1>Poglej datoteke</h1>
                                    <p><form method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>">
                                        <div>
                                        <table>
                                            <tr>
                                                <td>Mape:</td>
                                                <td><select name="vseMape">
                                                    <?php

                                                    foreach($vseMape as $mape) {
                                                        ?><option value="<?php echo $mape->getIdmapa(); ?>"><?php echo $mape->getImemapa() ?></option>; <?php
                                                    }

?>
                                                </select></td>
                                            </tr>
                                        </table>
                                		<p><input type="submit" id="izpisi" name="izpisi" value="Izpisi" /></p>
                                        </div>
                                	</form></p>
                                    
                                    <?php
                                if(!empty($datoteke)) {
                                    
                                    
                                    
                                    foreach($datoteke as $dato) {
                                        ?><a href="<?php echo "http://".$streznik."".$pot."/upload/".$_SESSION['uporabnik']['upIme']."/".$imeMape."/".$dato->getImedatoteka(); ?>"><?php echo $dato->getImedatoteka(); ?></a><br /><?php
                                    }
                                }
?>                              <br><br><br><h1>Nalozi</h1>
                                <p><form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data">
                                        <div>
                                        <table>
                                            <tr>
                                                <td>Mapa:</td>
                                                <td><select name="vseMape">
                                                    <?php

                                                    foreach($vseMape as $mape) {
                                                        ?><option value="<?php echo $mape->getIdmapa(); ?>"><?php echo $mape->getImemapa() ?></option>; <?php
                                                    }

?>
                                                </select></td>
                                            </tr>
                                            <tr>
                                                <td>Datoteka:</td>
                                                <td><input type="file" name="datoteka" id="datoteka" /> </td>
                                            </tr>
                                        </table>
                                		<p><input type="submit" id="nalozi" name="nalozi" value="Nalozi" /></p>
                                        </div>
                                	</form></p>
                                    <h1><?php if(isset($_GET['napaka'])) {
                                            if($_GET['napaka'] == 'niDatoteke') {
                                                echo "Niste izbrali datoteke<br><br>";
                                            } else if($_GET['napaka'] == 'jeDatoteka') {
                                                echo "Datoteko ste naložili<br><br>";
                                            } else if($_GET['napaka'] == 'zeObstaja') {
                                                echo "Datoteka že obstaja<br><br>";
                                            }
                                        } ?></h1>
                                    
    						</div>
    					</div>
    					
    					<div style="clear: both;">&nbsp;</div>
    				</div>
    				<!-- end #content -->
    				<div id="sidebar">
    					<ul>
    						
                            <?php

                                include_once('sidebar.php');

?>
                            
    						<!-- <li>
    							<h2>Categories</h2>
    							<ul>
    								<li><a href="#">Aliquam libero</a></li>
    								<li><a href="#">Consectetuer adipiscing elit</a></li>
    								<li><a href="#">Metus aliquam pellentesque</a></li>
    								<li><a href="#">Suspendisse iaculis mauris</a></li>
    								<li><a href="#">Urnanet non molestie semper</a></li>
    								<li><a href="#">Proin gravida orci porttitor</a></li>
    							</ul>
    						</li> -->
    					</ul>
    				</div>
    				<!-- end #sidebar -->
    				<div style="clear: both;">&nbsp;</div>
    			</div>
    		</div>
    	</div>
    	<!-- end #page -->
    </div>
    <?php  include_once("noga.php"); } ?>
    <!-- end #footer -->
</body>
</html>
